<html>
<head><meta charset="utf-8"><title>Can C++ catch and swallow a Rust panic · project-ffi-unwind · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/index.html">project-ffi-unwind</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Can.20C.2B.2B.20catch.20and.20swallow.20a.20Rust.20panic.html">Can C++ catch and swallow a Rust panic</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="185157068"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Can%20C%2B%2B%20catch%20and%20swallow%20a%20Rust%20panic/near/185157068" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Can.20C.2B.2B.20catch.20and.20swallow.20a.20Rust.20panic.html#185157068">(Jan 08 2020 at 21:09)</a>:</h4>
<p>Basically, C++ can catch a Rust panic with <code>catch (...)</code> and then decide not to rethrow it. <a href="https://github.com/rust-lang/rust/issues/67711" target="_blank" title="https://github.com/rust-lang/rust/issues/67711">#67711</a> makes sure that the <code>Box&lt;Any&gt;</code> is properly freed in this case, so we don't leak memory.</p>



<a name="185157184"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Can%20C%2B%2B%20catch%20and%20swallow%20a%20Rust%20panic/near/185157184" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Can.20C.2B.2B.20catch.20and.20swallow.20a.20Rust.20panic.html#185157184">(Jan 08 2020 at 21:10)</a>:</h4>
<p>However this does affect the way Rust handles double-panics. The problem is that <code>panic_count</code> in TLS is incremented when the panic is thrown, but not decremented when the panic is caught and destroyed by C++.</p>



<a name="185157203"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Can%20C%2B%2B%20catch%20and%20swallow%20a%20Rust%20panic/near/185157203" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Can.20C.2B.2B.20catch.20and.20swallow.20a.20Rust.20panic.html#185157203">(Jan 08 2020 at 21:10)</a>:</h4>
<p>This means that the next time a panic occurs, Rust will think that it is a double-panic and abort immediately.</p>



<a name="185157298"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Can%20C%2B%2B%20catch%20and%20swallow%20a%20Rust%20panic/near/185157298" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Can.20C.2B.2B.20catch.20and.20swallow.20a.20Rust.20panic.html#185157298">(Jan 08 2020 at 21:11)</a>:</h4>
<p>Now the question is, do we want to allow FFI code to catch and discard Rust panics?</p>



<a name="185157337"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Can%20C%2B%2B%20catch%20and%20swallow%20a%20Rust%20panic/near/185157337" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Can.20C.2B.2B.20catch.20and.20swallow.20a.20Rust.20panic.html#185157337">(Jan 08 2020 at 21:11)</a>:</h4>
<p>If yes, then we need to rework <code>panic_count</code> and the way double-panics work.</p>



<a name="185157398"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Can%20C%2B%2B%20catch%20and%20swallow%20a%20Rust%20panic/near/185157398" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Can.20C.2B.2B.20catch.20and.20swallow.20a.20Rust.20panic.html#185157398">(Jan 08 2020 at 21:12)</a>:</h4>
<p>If not then we can abort in the exception destructor if it is called outside of <code>catch_unwind</code>, just like <code>pthread_cancel</code> does.</p>



<a name="185157524"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Can%20C%2B%2B%20catch%20and%20swallow%20a%20Rust%20panic/near/185157524" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Can.20C.2B.2B.20catch.20and.20swallow.20a.20Rust.20panic.html#185157524">(Jan 08 2020 at 21:13)</a>:</h4>
<p>The main user-visible impact is that backtraces when a double-panic occurs will be slightly less precise: the second panic is treated as a normal panic (backtrace depends on RUST_BACKTRACE), but we will abort a bit later in the landing pad for the destructor which calls <code>abort</code> instead of <code>_Unwind_Resume</code>.</p>



<a name="185158395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Can%20C%2B%2B%20catch%20and%20swallow%20a%20Rust%20panic/near/185158395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Can.20C.2B.2B.20catch.20and.20swallow.20a.20Rust.20panic.html#185158395">(Jan 08 2020 at 21:22)</a>:</h4>
<p>I think we probably don't want to allow that, at least not now, but it's a good issue to raise</p>



<a name="185160700"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Can%20C%2B%2B%20catch%20and%20swallow%20a%20Rust%20panic/near/185160700" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Can.20C.2B.2B.20catch.20and.20swallow.20a.20Rust.20panic.html#185160700">(Jan 08 2020 at 21:50)</a>:</h4>
<p>Aborting in the exception destructor sounds like the right approach to me. That way C++ can still catch the exception as long as it re-throws it, correct?</p>



<a name="185161735"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Can%20C%2B%2B%20catch%20and%20swallow%20a%20Rust%20panic/near/185161735" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Amanieu <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Can.20C.2B.2B.20catch.20and.20swallow.20a.20Rust.20panic.html#185161735">(Jan 08 2020 at 22:01)</a>:</h4>
<p>Yes, C++ can catch and rethrow it. Just not catch and ignore it.</p>



<a name="185163537"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/210922-project-ffi-unwind/topic/Can%20C%2B%2B%20catch%20and%20swallow%20a%20Rust%20panic/near/185163537" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> BatmanAoD (Kyle Strand) <a href="https://rust-lang.github.io/zulip_archive/stream/210922-project-ffi-unwind/topic/Can.20C.2B.2B.20catch.20and.20swallow.20a.20Rust.20panic.html#185163537">(Jan 08 2020 at 22:23)</a>:</h4>
<p>Yep, that sounds good to me.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>